package com.moleq.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.activities.SaleActivity;
import com.moleq.mgpos.activities.R;
import com.moleq.service.ServiceManager;
import com.moleq.util.MoleQUtil;

public class DateSelectionDialog extends MoleQBaseDialog {

	private Button btnConfirmDate;
	private Button btnClose;
	private TextView tv_datetime;
	private DatePicker dp;
	private String age_time;
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			
			onCloseWithReturn(arg0.getId(),age_time);
			
			/*
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.buttondateClose:

				if (isPosLineType)
					MessageBox.show(mContext, mContext.getResources().getString(R.string.msg_not_old_enough_2));
				TransactionManager.getInstance().mSaleListItems.removeInvalidAgeItem(0);
				TransactionManager.getInstance().announceChanges();
				onCloseWithReturn("");
				break;
			case R.id.buttondateConfirm:

				TransactionManager.getInstance().setBirthdayDate(MoleQUtil.fixDateTime(age_time));

				int year = TransactionManager.getInstance().Age;

				if (TransactionManager.getInstance().mSaleListItems.checkage(year)) {
					if (isPosLineType) {
						MessageBox.show(mContext, mContext.getResources().getString(R.string.msg_not_old_enough_2));
						TransactionManager.getInstance().mSaleListItems.removeInvalidAgeItem(year);
					} else {
						MessageBox.show(mContext, mContext.getResources().getString(R.string.msg_not_old_enough_1));
					}
				} else {

					ServiceManager.getInstance().mPosTrnsService.UpdateBirthday(TransactionManager.getInstance().getBirthdayDate(), TransactionManager.getInstance().TransactionNo);
					// sa.keySearchItem(mSaleListitem.getUpc());

					sa.ProcessedAge();
				}

				onCloseWithReturn("");

				break;
			}
			*/
		}
	};


	public DateSelectionDialog(Context context) {
		super(context, R.style.dialog, context);
		constructor(context);
	}

	private void constructor(Context context) {
		mContext = context;
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_date_selection);
		this.setCancelable(false);

		btnConfirmDate = (Button) findViewById(R.id.buttondateConfirm);
		btnConfirmDate.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.buttondateClose);
		btnClose.setOnClickListener(mOnClickListener);

		tv_datetime = (TextView) findViewById(R.id.tv_datetime);
		dp = (DatePicker) findViewById(R.id.datePickerSelection);
	}

	public void show() {
		super.show();
		
		tv_datetime.setText("1980-01-01");
		age_time = "1980-01-01";
		dp.init(1980, 0, 1, new OnDateChangedListener() {

			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

				age_time = year + "-" + MoleQUtil.fixNumber3(monthOfYear + 1, "00") + "-" + MoleQUtil.fixNumber3(dayOfMonth, "00");
				tv_datetime.setText(age_time);

			}

		});

	}

}
